<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadController extends BaseController
{
    //上传图片
    public function uploadimage(Request $request){
        if($request->hasFile('file')){
            try {
                //上传文件到 指定位置
                $file = $request->file('file');
                if (!$file->isValid()) {
                    abort(400, '无效的上传文件');
                }
                //获取文件后缀
                $ext = $file->getClientOriginalExtension();
                //获取文件创建当前日期
                $date = date('Ymd');
                //新创建文件名及其后缀
                $newImgName = time().'.'.$ext;
                //构造目录 //图片存储根目录
                $tree = "uploads/".$date;
                if(file_exists($date)){
                    mkdir($tree,0777);
                }
                //将新文件移动至对应文件夹下
                $file->move($tree, $newImgName);
                $path = $tree.'/'.$newImgName;
                if($path){
                    $fileUrl = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].'/'.$path;
                    return  $this->reSuJson('上传成功',$fileUrl);
                }

            }catch (\Exception $e){
                return  $this->reErJson($e->getMessage());
            }
        }else{
            return $this->reErJson('非法请求');
        }
    }
}
